import { defineConfig } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import UniComponents from "@uni-helper/vite-plugin-uni-components";
// import { UniEchartsResolver } from "uni-echarts/resolver";
import TransformPages from "uni-read-pages-vite";
import AutoImport from "unplugin-auto-import/vite";
import postCssPxToRpx from "postcss-pxtorpx-pro";
import { babel } from "@rollup/plugin-babel";
import * as path from "node:path";

export default defineConfig({
    resolve: {
        alias: {
            "@": path.resolve(__dirname, "src"),
            "@uni-ui": path.resolve(__dirname, "src/custom-uni-ui/uni-ui/lib")
        }
    },
    envPrefix: "TTS_",
    plugins: [
        UniComponents({
            resolvers: [
                // UniEchartsResolver()
            ]
        }),
        uni(),
        AutoImport({
            imports: ["vue", "uni-app", "@vueuse/core"],
            dts: "src/types/auto-imports.d.ts", // 生成自动导入的声明文件
            eslintrc: {
                enabled: true // 生成eslint配置
            },
            resolvers: [
                // UniEchartsResolver()
            ]
        }),
        babel({
            babelHelpers: "runtime", // 改为与输出一致的runtime模式
            include: ["src/**/*.ts", "src/**/*.vue", "src/**/*.js"],
            extensions: [".ts", ".vue", ".js"],
            configFile: "./babel.config.js"
        })
    ],
    css: {
        preprocessorOptions: {
            scss: {
                silenceDeprecations: ["legacy-js-api"],
                // 启用源码映射以便调试
                sourceMap: true,
                // 其他 Sass 选项
                implementation: require("sass")
            }
        },
        postcss: {
            plugins: [
                postCssPxToRpx({
                    // 转化的单位
                    unit: "rpx",
                    // 单位精度
                    unitPrecision: 5,
                    // 不需要处理的css选择器
                    selectorBlackList: [],
                    // 不需要转化的css属性
                    propBlackList: [],
                    // 直接修改px，还是新加一条css规则
                    replace: true,
                    // 是否匹配媒介查询的px
                    mediaQuery: false,
                    // 需要转化的最小的pixel值，低于该值的px单位不做转化
                    minPixelValue: 0,
                    // 转化函数
                    // 默认设计稿按照750宽，2倍图的出
                    transform: (x) => x
                })
            ]
        }
    },
    build: {
        terserOptions: {
            compress: {
                drop_console: true,
                drop_debugger: true
            }
        }
    },
    define: {
        __APP_ROUTES: new TransformPages().getPagesRoutes()
    }
});
